package gui;
import java.awt.Color;
import java.awt.Container;
import java.awt.Point;
import java.util.Vector;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import modell.KaduModell;
import modell.Mitarbeiter;
import modell.PoolOfWorkers;
import controller.KaduController;
import controller.MyActionListener;
import controller.MyMenuListener;
import controller.MyMouseListener;




/**
 * @author Joachim
 * @use This class is the main GUI class. It is very important. Do not change anything unless you know what you do.
 * @param KaduController, KaduModell
 */
public class KaduGui extends JFrame{
	String frameTitle = "Kadu Personal Management";
	JMenu jmenu;
	JMenuItem jMenuItem, jMenuItemDatabase;
	public JFrame f,f2;
	public JPanel p,p2;
	JTextField tfIn,tfOut;
	JButton ok, cancel, hire;
	public PoolOfWorkers pool;
	public KaduModell kadumodell;
	public MyActionListener myal;
	public MyMenuListener myml;
	public MyMouseListener mymousel;
	public KaduController kaduController;
	public String listentry;
	public Vector<Mitarbeiter> vec ;
	public DefaultListModel dfm;
	public DynList dynList;
	public MyDialog myDiag;
	public JLabel listLabel;
	public JLabel labSumOfWorker;

	int f_pos_x = 200, f_pos_y = 100, f_siz_x = 600, f_siz_y = 600;
	int f2_pos_x = 200, f2_pos_y = 100, f2_siz_x = 600, f2_siz_y = 600;
	int p_pos_x = 0 , p_pos_y = 0, p_siz_x = 600, p_siz_y = 600;
	int p2_pos_x = 0 , p2_pos_y = 0, p2_siz_x = 600, p2_siz_y = 600;
	int panelList_pos_x = 380 , panelList_pos_y = 50, panelList_siz_x = 202, panelList_siz_y = 202;
	int tfIn_pos_x = 1, tfIn_pos_y = 35, tfIn_siz_x = 200 , tfIn_siz_y = 30;
	int tfOut_pos_x = 1, tfOut_pos_y = 70, tfOut_siz_x = 200 ,tfOut_siz_y = 30;
	int butOk_pos_x = 100, butOk_pos_y = 500,butOk_siz_x = 80, butOk_siz_y = 30;
	int butCancel_pos_x = 300, butCancel_pos_y = 500 , butCancel_siz_x = 80, butCancel_siz_y = 30;
	int butFireWorker_pos_x = 425, butFireWorker_pos_y = 285 , butFireWorker_siz_x = 120, butFireWorker_siz_y = 30;
	int butshowThisSelectedWorker_pos_x = 425, butshowThisSelectedWorker_pos_y = 315 , butshowThisSelectedWorker_siz_x = 120, butshowThisSelectedWorker_siz_y = 30;
	int butMulti_pos_x = 430, butMulti_pos_y = 500 , butMulti_siz_x = 120, butMulti_siz_y = 30;
	int butHire_pos_x = 425, butHire_pos_y = 255 , butHire_siz_x = 120, butHire_siz_y = 30;
	int butEditWorker_pos_x = 425, butEditWorker_pos_y = 345 , butEditWorker_siz_x = 120, butEditWorker_siz_y = 30;
	int jtree_pos_x = 500, jtree_pos_y = 10, jtree_siz_x = 100 ,jtree_siz_y = 200;
	int jmenu_pos_x = 0, jmenu_pos_y = 0, jmenu_siz_x = 50 ,jmenu_siz_y = 30;
	int jMenuBar_pos_x = 0, jMenuBar_pos_y = 0, jMenuBar_siz_x = 500 ,jMenuBar_siz_y = 40;
	Point labSumOfWorker_siz = new Point (30,140);
	Point labSumOfWorker_pos = new Point (530,-30);


	public KaduGui(KaduController kaduController,KaduModell kadumodell){
		this.kaduController = kaduController;
		this.kadumodell = kadumodell;
		initGui();
	}

	public KaduGui() {
		initGui();
		System.out.println("aufruf von kadugui ohne parameter kadumodell");
		// TODO Auto-generated constructor stub
	}

	public KaduGui(KaduController kaduController, KaduModell kadumodell2,
			PoolOfWorkers poolOfWorkers) {
		this.kadumodell = kadumodell2;
		this.kaduController = kaduController;
		System.out.println("aufruf von kadugui mit parameter kadumodell und kaduController");
		initGui();
	}

	public void initGui(){
		System.out.println("Programmflusskontrolle: Beginn KaduGui-initGui");
		// **********************
		// Creating Listeners ???????????   <-  ? 
		// **********************
		myal = new MyActionListener(this);
		mymousel = new MyMouseListener(this);
		myml = new MyMenuListener(this);
		// ************************************************	
		// *  		   		Now the Menu                  *	
		// ************************************************	
		JMenuBar jMenuBar = new JMenuBar();
		jMenuBar.setSize(300, 30);
		jMenuBar.setLocation(1, 1);
		jMenuBar.setVisible(true);
		JMenu jmenu = new JMenu("Mitarbeiter");
		jmenu.setSize(jmenu_pos_x, jmenu_pos_y);
		jmenu.setLocation(jmenu_pos_x, jmenu_pos_y);

		JMenuItem addWorker = new JMenuItem();
		addWorker.setText("Add");
		addWorker.setSize(jmenu_pos_x, jmenu_pos_y);
		addWorker.setLocation(jmenu_pos_x, jmenu_pos_y);
		addWorker.setText("Add a worker");
		addWorker.setActionCommand("Add a worker");
		addWorker.addActionListener(myml);

		JMenuItem zeigeAn = new JMenuItem("Show available workers");
		zeigeAn.setSize(150, 30);
		zeigeAn.setText("Show available workers");
		zeigeAn.setVisible(true);
		zeigeAn.setActionCommand("Show available workers");
		zeigeAn.addActionListener(myml);
		
		JMenuItem jMenuItemDatabase = new JMenuItem("jMenuItemDatabase");
		jMenuItemDatabase.setSize(150, 30);
		jMenuItemDatabase.setText("jMenuItemDatabase");
		jMenuItemDatabase.setVisible(true);
		jMenuItemDatabase.setActionCommand("jMenuItemDatabase");
		jMenuItemDatabase.addActionListener(myml);
		

		JFrame f = new JFrame(frameTitle);
		f.setLocation(f_pos_x, f_pos_y);
		f.setVisible(true);
		f.setSize(f_siz_x,f_siz_y);
		f.setDefaultCloseOperation(EXIT_ON_CLOSE);

		JPanel p = new JPanel();
		p.setVisible(true);
		p.setLocation(p_pos_x, p_pos_y);
		p.setSize(p_siz_x,p_siz_y);
		// a panel for the list

		JPanel panelList = new JPanel();
		panelList.setVisible(true);
		panelList.setLayout(null);
		panelList.setBackground(Color.lightGray);
		panelList.setLocation(panelList_pos_x, panelList_pos_y);
		panelList.setSize(panelList_siz_x,panelList_siz_y);

		JButton ok = new JButton("ok");
		ok.setSize(butOk_siz_x,butOk_siz_y);
		ok.setLocation(butOk_pos_x,butOk_pos_y);

		JButton hire = new JButton("Hire");
		hire.setSize(butHire_siz_x,butHire_siz_y);
		hire.setLocation(butHire_pos_x,butHire_pos_y);


		JButton multi = new JButton("multi");
		multi.setSize(butMulti_siz_x,butMulti_siz_y);
		multi.setLocation(butMulti_pos_x,butMulti_pos_y);

		JButton fireWorker = new JButton("Fire");
		fireWorker.setSize(butFireWorker_siz_x,butFireWorker_siz_y);
		fireWorker.setLocation(butFireWorker_pos_x,butFireWorker_pos_y);
		fireWorker.setActionCommand("fireWorker");
		
		JButton edit = new JButton("Edit");
		edit.setSize(butEditWorker_siz_x,butEditWorker_siz_y);
		edit.setLocation(butEditWorker_pos_x,butEditWorker_pos_y);
		edit.setActionCommand("edit");

		JButton showThisSelectedWorker = new JButton("Show");
		showThisSelectedWorker.setSize(butshowThisSelectedWorker_siz_x,butshowThisSelectedWorker_siz_y);
		showThisSelectedWorker.setLocation(butshowThisSelectedWorker_pos_x,butshowThisSelectedWorker_pos_y);
		showThisSelectedWorker.setActionCommand("showThisSelectedWorker");


		JButton cancel = new JButton("cancel");
		cancel.setSize(butCancel_siz_x,butCancel_siz_y);
		cancel.setLocation(butCancel_pos_x,butCancel_pos_y);

		JTextField tfIn = new JTextField();
		tfIn.setVisible(true);
		tfIn.setLocation(tfIn_pos_x, tfIn_pos_y);
		tfIn.setSize(tfIn_siz_x,tfIn_siz_y);

		JTextField tfOut = new JTextField();
		tfOut.setVisible(true);
		tfOut.setLocation(tfOut_pos_x, tfOut_pos_y);
		tfOut.setSize(tfOut_siz_x,tfOut_siz_y);
		dfm = kadumodell.myListModell;
		dynList = new DynList(this, dfm);
		//myDiag = null;
		

		listLabel = new JLabel("List of Workers");
		listLabel.setSize(140,20);
		listLabel.setLocation((panelList_pos_x+55),(panelList_pos_y-20));
		
		int i = kadumodell.pool.vector.size();
		labSumOfWorker = new JLabel("("+i+")");
		labSumOfWorker.setSize(labSumOfWorker_siz.x,labSumOfWorker_siz.y);
		labSumOfWorker.setLocation(labSumOfWorker_pos.x,labSumOfWorker_pos.y);
		labSumOfWorker.setVisible(true);
		p.add(labSumOfWorker);

		// to enable scrolling within the list...
		//... i create a JScrollPane an hand over the list as parameter
		JScrollPane listScroller = new JScrollPane(dynList);
		listScroller.setBackground(Color.BLACK);
		listScroller.setBorder(null);
		listScroller.setSize(200,200);
		listScroller.setLocation(1,1);
		listScroller.setVisible(true);
		dynList.clearTheList();
		dynList.repaint();
		dynList.updateUI();
		// **********************
		// Adding Components to the panel IAIIIAIII   <-  A 
		// **********************
		jmenu.add(addWorker);
		jmenu.add(zeigeAn);
		jmenu.add(jMenuItemDatabase);
		jMenuBar.add(jmenu);
		p.add(panelList);
		p.add(jMenuBar);
		p.add(ok);
		p.add(hire);
		p.add(multi);
		p.add(fireWorker);
		p.add(showThisSelectedWorker);
		p.add(edit);
		p.add(cancel);
		p.add(tfIn);
		p.add(tfOut);
		p.add(listLabel);
		p.setLayout(null);
		f.setLayout(null);
		// a panel for the scrollpane containing the list
		panelList.add(listScroller);

		Container content = f.getContentPane();
		content.add(p);
		// **********************
		// Adding ActionListeners ??????????  <-  ? 
		// **********************
		fireWorker.addActionListener(myal);
		showThisSelectedWorker.addActionListener(myal);
		ok.addActionListener(myal);
		cancel.addActionListener(myal);
		hire.addActionListener(myal);
		multi.addActionListener(myal);
		edit.addActionListener(myal);

		// mouselisteners
		f.addMouseListener(mymousel);
		repaint();
		//setIT("hzh");
		p.updateUI();
		System.out.println("Programmflusskontrolle: Ende KaduGui-initGui vor console");
		Console console = new Console();
		try {
			console.frankfurt(null);
		} catch (Exception e) {
			e.printStackTrace();
		}

		System.out.println("Programmflusskontrolle: Ende KaduGui-initGui nach console");
	}
	public void setIT(String name){
		dynList.addElementTotheList(name);
		System.out.println("added + " +name);
	}
	private static final long serialVersionUID = 1L;

	//	public void showThisSelectedWorkerOnScreen() {
	//		// TODO Auto-generated method stub
	//		if (dynList.jlist1.isSelectionEmpty()==false){
	//			int showME = dynList.jlist1.getSelectedIndex();
	//			//myDiag = new MyDialog((showME+1));
	//			System.out.println("war hier");
	//			f2 = new JFrame("jojo");
	//			f2.setLocation(f2_pos_x, f2_pos_y);
	//			f2.setVisible(true);
	//			f2.setSize(f2_siz_x,f2_siz_y);
	//			f2.setLayout(null);
	//		}
	//		//f2.setDefaultCloseOperation(EXIT_ON_CLOSE);
	//
	//
	//	}
	public void addWorkerShow(){
		//int selectedInList = dynList.jlist1.getSelectedIndex();
		myDiag = new MyDialog(this,0,"add");
	}

	public void createIT(){
		if (dynList.jlist1.isSelectionEmpty()==false){
			int showME = dynList.jlist1.getSelectedIndex();
			myDiag = new MyDialog(this,showME,"show");
		}
	}

	public void fillTestData(){
		System.out.println(" fillTestData()");
		for (int i =0;i<10;i++){
			String ned = ""+i;
			Mitarbeiter einMitarbeiter = new Mitarbeiter(	"birth".concat(ned)
					,"dead".concat(ned) 
					,"name".concat(ned)
					,"surname".concat(ned) 
					,"weight".concat(ned)
					,"heigh".concat(ned) 
					,"sick".concat(ned)
					,"concreteJob".concat(ned) 
					,"dateJoined".concat(ned) 
					,"dateFired".concat(ned) 
					,"reasonJoin".concat(ned) 
					,"reasonFired".concat(ned) 
					,"menschPermissions".concat(ned));
			kaduController.neuenMitarbeiterErstellen(einMitarbeiter);
		}
		
	}	public void showSumOfWorkers(){
		
			System.out.println("ok");
		int i = kadumodell.pool.vector.size();
		labSumOfWorker.setText("("+i+")");
		
//		labSumOfWorker = new JLabel();
//		labSumOfWorker.setSize(labSumOfWorker_siz.x,labSumOfWorker_siz.y);
//		labSumOfWorker.setLocation(labSumOfWorker_pos.x,labSumOfWorker_pos.y);
//		labSumOfWorker.setVisible(true);
//		p.add(labSumOfWorker);
//		p.repaint();
//		System.out.println("ok2");
		
	}
}
